/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import bean.AddProviderBean;
import bean.LoginActionFormBean;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.AddProvider;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import pojo.FtpInfo;
import pojo.ActivityHistory;
import pojo.Provider;
import pojo.Scheduledata;

/**
 *
 * @author javauser
 */
public class AddProviderAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        AddProviderBean mybean = (AddProviderBean) form;
        String username = ((LoginActionFormBean) request.getSession(true).getAttribute("LoginActionFormBean")).getUserName();
        Provider prov = new Provider();
        prov.setName(mybean.getProviderName());
        prov.setType(mybean.getType());
        prov.setTableName(mybean.getTableName());
        prov.setScriptPath(mybean.getScriptPath());

        FtpInfo ftpinfo = new FtpInfo();
        ftpinfo.setHost(mybean.getHost());
        ftpinfo.setRemoteDir(mybean.getRemoteDir());
        ftpinfo.setFileName(mybean.getFileName());
        ftpinfo.setFileExtension(mybean.getFileExtension());
        ftpinfo.setUserName(mybean.getUserName());
        ftpinfo.setPassword(mybean.getPassword());
        ftpinfo.setDateMod("null");
        ftpinfo.setLocalDir(mybean.getLocalDir());

        Scheduledata scdata = new Scheduledata();
        scdata.setSchedule(mybean.getSchedule());
        scdata.setModifier(mybean.getModifier());
        scdata.setStartDate(mybean.getStartDated());
        scdata.setStartTime(mybean.getStartTime());
        if(mybean.getSchedule().equalsIgnoreCase("hourly")||mybean.getSchedule().equalsIgnoreCase("daily")){
            mybean.setDay(null);
        }
        if(mybean.getDay() != null){
            String[] selectedDays = mybean.getDay();
            String days = "";
            for (int i = 0; i < selectedDays.length; i++) {
                days += selectedDays[i] + ",";
            }
            int strlen = days.length();
            int lastindex = strlen - 1;
            String taskdays = days.substring(0, lastindex);
            scdata.setDay(taskdays);
        }
        else{
            scdata.setDay(null);
        }
        if(mybean.getMonth() != null){
            String[] selectedMonths = mybean.getMonth();
            String months = "";
            for (int i = 0; i < selectedMonths.length; i++) {
                months += selectedMonths[i] + ",";
            }
            int monthslen = months.length();
            int lastmonthsindex = monthslen - 1;
            String taskmonths = months.substring(0, lastmonthsindex);
            scdata.setMonth(taskmonths);
        }
        else{
            scdata.setMonth(null);
        }
        scdata.setIsenable(true);
        scdata.setIsrunning(false);
        
        ActivityHistory log = new ActivityHistory();
        log.setStatus("Success");
        log.setAdminName(username);
        log.setProviderName(mybean.getProviderName());

        AddProvider ap = new AddProvider(username);
        ap.addProvider(getDataSource(request, "MyDS").getConnection(), prov, ftpinfo, scdata, log, mybean.getColumnnames(), mybean.getColumntypes(), mybean.getAccuracylevels(), mybean.getRequired());
        return mapping.findForward(SUCCESS);
    }
}
